﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// 部门管理
/// </summary>
public partial class SystemManage_DepartMentManage : GH.Web.BasePage.PageBase
{
    static string UnitRoot = System.Configuration.ConfigurationSettings.AppSettings["UnitRoot"];

    #region 画面初始化

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TreeViewInit();
            SetGridShow(hdUnion.Value, 1);
        }
        else
        {
            SetGridShow(hdUnion.Value, 1);
        }
    }

    #endregion

    #region 画面方法

    private void SetGridShow(string strUnionID, int pageIndex)
    {
        GH.BLL.DepartMent departmentBll = new GH.BLL.DepartMent();
        string strWhere = "1=1";
        if (strUnionID != "-1")
        {
            strWhere = " 单位ID=" + strUnionID;
        }
        DataTable dtQuery = departmentBll.GetPageList(this.AspNetPager1.PageSize, pageIndex, strWhere).Tables[0];
        this.AspNetPager1.RecordCount = departmentBll.GetCount(strWhere);
        if (dtQuery.Rows.Count != 0)
        {
            rptList.DataSource = dtQuery;
            rptList.DataBind();
        }
        else
        {
            rptList.DataSource = null;
            rptList.DataBind();
        }
    }

    private void TreeViewInit()
    {
        trvUnion.Nodes.Clear();
        TreeNode Root = new TreeNode(UnitRoot, "-1");
        GH.BLL.Union unionBll = new GH.BLL.Union();
        TreeNode tNode;
        System.Text.StringBuilder strWhere = new System.Text.StringBuilder();        
        //部门操作权限
        if (!this.GetRight(102))
        {
            strWhere.AppendFormat(" 工会单位信息.ID={0}", int.Parse(Session["Union"].ToString()));
        }
        else
        {
            strWhere.AppendFormat("");
        }
        DataTable dtQuery = unionBll.GetList(strWhere.ToString()).Tables[0];

        if (dtQuery.Rows.Count != 0)
        {
            for (int i = 0; i < dtQuery.Rows.Count; i++)
            {
                tNode = new TreeNode();
                tNode.Text = dtQuery.Rows[i]["单位名称"].ToString();
                tNode.Value = dtQuery.Rows[i]["ID"].ToString();
                //trvUnion.Nodes.Add(tNode);
                Root.ChildNodes.Add(tNode);
            }
            trvUnion.Nodes.Add(Root);
            trvUnion.Nodes[0].Select();
            if (trvUnion.Nodes[0].ChildNodes.Count == 1)
            {
                hdUnion.Value = trvUnion.Nodes[0].ChildNodes[0].Value;
            }
            else
            {
                hdUnion.Value = trvUnion.Nodes[0].Value;
            }
            
        }
    }


    #endregion

    #region 画面事件

    protected void trvUnion_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (trvUnion.Nodes[0].ChildNodes.Count == 1)
        {
            hdUnion.Value = trvUnion.Nodes[0].ChildNodes[0].Value;
            return;
        }
        hdUnion.Value = trvUnion.SelectedNode.Value;
        this.AspNetPager1.CurrentPageIndex = 1;
        SetGridShow(hdUnion.Value, 1);

        string path = this.hidPath.Value;
        string theValue = this.hidText.Value;

        if (path != "")
        {
            TreeNode node = trvUnion.FindNode(path);
            node.Text = theValue;
        }
        path = this.trvUnion.SelectedNode.ValuePath;

        theValue = this.trvUnion.SelectedNode.Text;

        this.trvUnion.SelectedNode.Text = "<b>" + this.trvUnion.SelectedNode.Text + "</b>";

        this.hidPath.Value = path;
        this.hidText.Value = theValue;

    }

    protected void btnEdit_Click(object sender, ImageClickEventArgs e)
    {
        this.Response.Redirect("DepartMentManageEdit.aspx?Action=Add&UnionID=" + hdUnion.Value);
    }

    protected void trvUnion_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        e.Node.ImageUrl = "~/Images/closed_r.gif";
    }

    protected void trvUnion_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count != 0)
        {
            e.Node.ImageUrl = "~/Images/open_r.gif";
        }
    }

    #endregion

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
     
        int page = e.NewPageIndex;
        this.AspNetPager1.CurrentPageIndex = page;

        SetGridShow(hdUnion.Value, page);
    }

}
